/**
 * 
 */
package com.local.gson.gsonDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**

 * @author admin
 *  Gson生成器、解析器
 *  生成器:gson.toJson
 *  解析器:gson.fromJson
 *  new TypeToken<List<HashMap<String,Object>>>(){}.getType()
 *  TypeToken<>(){}可以是List<String>、List<JavaBean>类似于List<dto>、List<HashMap<String,Object>>
 *  注意：只要是object中的类型为number类型全部会转为double型
 */

class Gson_class {
	public List<HashMap<String,Object>> getGson(){
		List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
		for(int i = 0; i < 3; i++) {
			HashMap<String,Object> hash = new HashMap<String,Object>();
			switch(i) {
			case 0:
				hash.put(i+"", "阿雪");
				//hash.put(i+"", i);


				break;
			case 1:
				hash.put(i+"", "小静");
				//hash.put(i+"", i);


				break;
			case 2:
				hash.put(i+"", "小月");
				//hash.put(i+"", i);


				break;
			}
			list.add(hash);	
		}
		return list;
	}
}




public class GsonDemo {

	
	public void reflectList() {
		Gson_class gc = new Gson_class();
		Gson gson = new Gson();
		String str = gson.toJson(gc.getGson());//转换成String

		System.out.println("List<HashMap<String,Object>>转String:"+str);
		List<HashMap<String,Object>> list2 = gson.fromJson(str, 
				new TypeToken<List<HashMap<String,Object>>>(){}.getType());//还原回List<HashMap<String,Object>>

		System.out.println("一开始生成的List<HashMap<String,Object>>:"+gc.getGson());
		System.out.println("String转<HashMap<String,Object>>:"+list2);
	}
	
	/**

	 * @param args

	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub


		GsonDemo demo =  new GsonDemo();
		demo.reflectList();
		
	}

}
